package net.sudot.chess.business.wechat.message;

import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import net.sudot.chess.business.dto.ChatMessageDTO;
import net.sudot.chess.business.model.Customer;
import net.sudot.chess.business.model.User;
import net.sudot.chess.config.SystemProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 信息通知
 * <pre>
 * {{first.DATA}}
 * 消息类型：{{keyword1.DATA}}
 * 发送状态：{{keyword2.DATA}}
 * 发送时间：{{keyword3.DATA}}
 * 发送对象：{{keyword4.DATA}}
 * {{remark.DATA}}
 * <p/>
 * 最终发送示例:
 * <p/>
 * xxx，您有新的信息未处理
 *
 * 消息类型：@提醒
 * 发送状态：待处理
 * 发送时间：2018-04-29 23:10
 * 发送对象：测试用户
 *
 * 点击进入聊天室
 * </pre>
 *
 * @author tangjialin on 2018-04-29.
 */
@Component
public class InformationTemplateMessage extends AbstractTemplateMessage {

    @Value("${system.config.wechat.informationTemplateId}")
    private String templateId;

    @Override
    public String getTemplateId() {
        return templateId;
    }

    @Override
    protected WxMpTemplateMessage.WxMpTemplateMessageBuilder builderTemplateMessage(User user, Object data, SystemProperties systemProperties) {
        Customer customer = (Customer) user;
        ChatMessageDTO chatMessageDTO = (ChatMessageDTO) data;
        List<WxMpTemplateData> templateData = new ArrayList<>();
        templateData.add(new WxMpTemplateData("first", customer.getNickname() + "，您有新的信息未处理\n"));
        templateData.add(new WxMpTemplateData("keyword1", "[结束]".equals(chatMessageDTO.getContent()) ? "申请结束游戏" : "@提醒"));
        templateData.add(new WxMpTemplateData("keyword2", "待处理"));
        templateData.add(new WxMpTemplateData("keyword3", formatDate(OffsetDateTime.now())));
        templateData.add(new WxMpTemplateData("keyword4", chatMessageDTO.getNickname()));
        templateData.add(new WxMpTemplateData("remark", "\n点击进入聊天室"));
        WxMpTemplateMessage.WxMpTemplateMessageBuilder builder = WxMpTemplateMessage.builder().data(templateData);
        builder.url(systemProperties.getWebsite() + "/room/chat");
        return builder;
    }

}
